package com.uxbee.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.uxbee.pojo.HtmlData;

/**
 *
 * @author Administrator
 */
public class ConnectionManager {

	private static Connection con = null;
	
	private static PreparedStatement pstmt = null;
	
	private static String sql = "INSERT INTO `html_data` (`content` , `time` , `url` , `mainUrl` , `md5` , `level` , `finish` , `errorCount` ) VALUES (?, ?, ?, ?, ?, ?, ?, ?);";

	private static boolean autoCommit = true;

	public ConnectionManager() {

	}

	/**
	 * 获得数据库连接
	 */
	public static Connection getConnection() {
		try {
			if (con != null){
				return con;
			}
			String url = "jdbc:mysql://localhost:3306/sohuNews?useUnicode=true&characterEncoding=UTF-8";
			String usr = "root";
			//String pwd = "root";
			Class.forName("com.mysql.jdbc.Driver").newInstance();
			con = DriverManager.getConnection(url, usr, "");
			con.setAutoCommit(autoCommit);
		}
		catch (SQLException ex) {
			Logger.getLogger(ConnectionManager.class.getName()).log(
					Level.SEVERE, null, ex);
		}
		catch (InstantiationException ex) {
			Logger.getLogger(ConnectionManager.class.getName()).log(
					Level.SEVERE, null, ex);
		}
		catch (IllegalAccessException ex) {
			Logger.getLogger(ConnectionManager.class.getName()).log(
					Level.SEVERE, null, ex);
		}
		catch (ClassNotFoundException ex) {
			Logger.getLogger(ConnectionManager.class.getName()).log(
					Level.SEVERE, null, ex);
		}
		return con;
	}

	/**
	 *关闭数据库连接
	 */
	public static void close() {
		if (con != null) {
			try {
				pstmt.close();
				con.close();
			}
			catch (Exception e) {
				e.printStackTrace();
			}
			finally {
				con = null;
			}
		}
	}
	
	// 保存到数据库
	public static void saveToDB(HtmlData htmlData) {
		Connection connection = getConnection();
		try {
			if (pstmt == null){
				pstmt = connection.prepareStatement(sql);
			}
			if (htmlData.getContent() == null){
				pstmt.setString(1, "");
			}else{
				pstmt.setString(1, htmlData.getContent());
			}
			if (htmlData.getTime() == null){
				pstmt.setString(2, "");
			}else{
				pstmt.setString(2, htmlData.getTime().toString());
			}
			if (htmlData.getUrl() == null){
				pstmt.setString(3, "");
			}else{
				pstmt.setString(3, htmlData.getUrl());
			}
			if (htmlData.getMainUrl() == null){
				pstmt.setString(4, "");
			}else{
				pstmt.setString(4, htmlData.getMainUrl());
			}
			if (htmlData.getMd5() == null){
				pstmt.setString(5, "");
			}else{
				pstmt.setString(5, htmlData.getMd5());
			}
			if (htmlData.getLevel() == null){
				pstmt.setInt(6, -1);
			}else{
				pstmt.setInt(6, htmlData.getLevel());
			}
			if (htmlData.getFinish() == null){
				pstmt.setString(7, "");
			}else{
				pstmt.setString(7, htmlData.getFinish());
			}
			if (htmlData.getErrorCount() == null){
				pstmt.setInt(8, -1);
			}else{
				pstmt.setInt(8, htmlData.getErrorCount());
			}
			
			pstmt.execute();
		}
		catch (SQLException ex) {
			System.out.println(ex);
		}
	}
}
